package sweetcamera;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.NumberKeyListener;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.MyHongbaoPayDialogActivity;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.fragment.PeopleTimeLineFragment;
import com.github.postsanf.yinian.utils.BigDecimalUtil;
import com.github.postsanf.yinian.utils.CashierInputFilter;
import com.github.postsanf.yinian.utils.StringUtils;

public class CameraPublishHongbaoActivity extends Activity implements View.OnClickListener {
    private EditText hongbaojiner_num;
    private EditText hongbaogeshu_num;
    private InputMethodManager imm;
    private TextView price_show;
    private TextView members_num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camerapublish_hongbao);
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        registerBoradcastReceiver();
        initView();
    }

    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionCloseactivity);
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionCloseactivity:
                    finish();
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.iv_back_tx).setOnClickListener(this);
        findViewById(R.id.hongbao_sure).setOnClickListener(this);
        members_num = (TextView) findViewById(R.id.members_num);
        members_num.setText(String.valueOf(PeopleTimeLineFragment.MembersNum - 1));
        price_show = (TextView) findViewById(R.id.price_show);
        hongbaojiner_num = (EditText) findViewById(R.id.hongbaojiner_num);
        hongbaogeshu_num = (EditText) findViewById(R.id.hongbaogeshu_num);
        hongbaojiner_num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hongbaojiner_num.setSelection(hongbaojiner_num.getText().length());
            }
        });
        hongbaogeshu_num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hongbaogeshu_num.setSelection(hongbaogeshu_num.getText().length());
            }
        });
        InputFilter[] inputFilters = {new CashierInputFilter()};
        hongbaojiner_num.setFilters(inputFilters);
        hongbaogeshu_num.setKeyListener(new NumberKeyListener() {
            @Override
            protected char[] getAcceptedChars() {
                return new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
            }

            @Override
            public int getInputType() {
                return InputType.TYPE_CLASS_PHONE;
            }
        });
        hongbaogeshu_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    if (Integer.parseInt(s.toString()) > 100000) {
                        Toast.makeText(CameraPublishHongbaoActivity.this, "已超过最大个数", Toast.LENGTH_SHORT).show();
                        String thisNum = hongbaogeshu_num.getText().toString();
                        hongbaogeshu_num.setText(thisNum.substring(0, thisNum.length() - 1));
                        hongbaogeshu_num.setSelection(hongbaogeshu_num.getText().length());
                        return;
                    }
                } else {
                    price_show.setText("0");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        hongbaojiner_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    price_show.setText(s);
                    if (Double.parseDouble(s.toString()) > 200) {
                        Toast.makeText(CameraPublishHongbaoActivity.this, "红包金额最大200", Toast.LENGTH_SHORT).show();
                        hongbaojiner_num.setText("200");
                        price_show.setText("200");
                        hongbaojiner_num.setSelection(hongbaojiner_num.getText().length());
                        return;
                    }
                } else {
                    price_show.setText("0");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
            case R.id.iv_back_tx:
                finish();
                break;
            case R.id.hongbao_sure:
                String totalPrice = hongbaojiner_num.getText().toString().trim();
                String totalgeshu = hongbaogeshu_num.getText().toString().trim();
                if (StringUtils.isEmpty(totalPrice) || totalPrice.equals("0")) {
                    Toast.makeText(this, "输入金额为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (StringUtils.isEmpty(totalgeshu) || totalgeshu.equals("0")) {
                    Toast.makeText(this, "输入个数为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                int zongPrice = BigDecimalUtil.multotal(totalPrice, "100");
                Log.i("xjs", "zongPrice==>" + zongPrice);
                if (zongPrice <= 0) {
                    Toast.makeText(this, "金额有误", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (BigDecimalUtil.multotal(totalgeshu, "1") <= 0) {
                    Toast.makeText(this, "输入个数有误", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (zongPrice / Integer.parseInt(totalgeshu) < 1) {
                    Toast.makeText(CameraPublishHongbaoActivity.this, "请修改红包个数，每人平均金额不得小于0.01", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent mIntent = new Intent(this, MyHongbaoPayDialogActivity.class);
                mIntent.putExtra("totalPrice", totalPrice);
                mIntent.putExtra("totalgeshu", totalgeshu);
                startActivity(mIntent);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                break;
        }
    }
}
